/**
 * create by young (young2f@outlook.com) on 2024/4/21
 */
'use strict';
const base = require('../base/controllerBase');
const _service = 'procureService';
const _ = require('lodash');

class ProcureController extends base {
  async editOld() {
    // const { ctx, service } = this;
    // try {
    //   const { body } = ctx.request;
    //   const result = await service[_service].editOld(body);
    //   ctx.body = result;
    //   ctx.status = 200;
    // } catch (e) {
    //   console.error(e);
    //   const _message = e && e.message ? e.message : this.ERROR_MSG;
    //   ctx.body = { status: 'error', message: _message, result: e };
    //   ctx.status = 200;
    // }
  }

  async create() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };

      ctx.validate(model, body);

      const result = await service[_service].create(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async updateOne() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };

      ctx.validate(model, body);
      const result = await service[_service].updateOne(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async deleteOne() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };

      ctx.validate(model, body);
      const result = await service[_service].deleteOne(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async review() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };

      ctx.validate(model, body);
      const result = await service[_service].review(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findProcureMaterialLot() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        pmLotId: { type: 'string' },
      };

      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].findProcureMaterialLot(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  /**
   * 查找可用的采购物料，不是批次，是多个相同物料批次合并成一个物料
   * @returns {Promise<void>}
   */
  async findUsedProcureMaterials() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;

      const result = await service[_service].findUsedProcureMaterials(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  /**
   * 查找可用的采购物料批次，根据物料查
   * @returns {Promise<void>}
   */
  async findUsedProcureMaterialLots() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };
      console.log('findUsedProcureMaterialLots', body);
      ctx.validate(model, body);
      const result = await service[_service].findUsedProcureMaterialLots(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findOne() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };

      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].findOne(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findMany() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };

      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].findMany(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  /**
   * 查询采购物料
   * code
   * :
   * undefined
   * createdTs
   * :
   * (2) [Moment, Moment]
   * reviewStatus
   * :
   * undefined
   * vendor
   * :
   * undefined
   * @returns {Promise<void>}
   */
  async findProcureMaterialRecordMany() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };
      console.log('findProcureMaterialRecordMany', body);
      ctx.validate(model, body);
      body.isDeleted = false;

      const result = await service[_service].findProcureMaterialRecordMany(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findProcureMaterialRecordByProcureOrderCodeMany() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        procureOrderCode: { type: 'string' },
      };
      console.log('findProcureMaterialRecordMany', body);
      ctx.validate(model, body);
      body.isDeleted = false;

      const result = await service[_service].findProcureMaterialRecordMany(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async procureMaterialLotAssess() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };
      console.log('procureMaterialLotAssess', body);
      ctx.validate(model, body);
      body.isDeleted = false;

      const result = await service[_service].procureMaterialLotAssess(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async createProcureMaterial() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        pmLotId: { type: 'string' },
      };
      console.log('update material', body);
      ctx.validate(model, body);
      const result = await service[_service].createMaterial(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findProcureMaterialUseSalesOrder() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        pmLotId: { type: 'string' },
      };
      console.log('findProcureMaterialUseSalesOrder BODY', body);
      ctx.validate(model, body);
      const result = await service[_service].findProcureMaterialUseSalesOrder(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findStatistic() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };
      console.log('find procure many', body);
      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].findStatistic(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async createReturnGoodsOrder() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };
      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].createReturnGoodsOrder(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }
  async deleteReturnGoodsOrder() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        orderCode: { type: 'string' },
      };
      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].deleteReturnGoodsOrder(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async updateReturnGoodsOrder() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        orderCode: { type: 'string' },
      };
      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].updateReturnGoodsOrder(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findReturnOrderMany() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };
      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].findReturnOrderMany(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async export() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };

      ctx.validate(model, body);
      const result = await service[_service].exportExcel(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

}

module.exports = ProcureController;
